#include "logger.h"
#include "ccreader.h"

#if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
    #include <windows.h>
#else
    #include <cstdio>
#endif
#include <iostream>
#include <QTextStream>

//#include <sstream>
namespace Logg
{
Logger::Logger(QObject *parent) :  ILoop(true)
{
    LogStr[0]="ERR";
    LogStr[1]="INF";
    LogStr[2]="SYS";
    LogStr[3]="ALR";
    LogStr[4]="DEB";
    LogStr[5]="DB<";
    LogStr[6]="DB>";
    LogStr[7]="UD<";
    LogStr[8]="UD>";
}

void Logger::run(){
    LogIt(tr("Logger Iniciado!"));
    cre = new CCReader(this->parent());
    cre->start();

    while(ILoop)
    {
        if(logs.count()>0)// && logs[0].text.size()>0)
        {
            QMutexLocker locker(&mutex);
            ShowLog(logs[0]);
            logs.removeAt(0);
        }
    }
}

void Logger::LogIt(QString text, LogTypes type)
{
    QMutexLocker locker(&mutex);
    LogItem nlog;
    QString tt;
        tt.append(text);
        nlog.text = tt;
        nlog.type = type;
        logs << nlog;
}

void Logger::ShowLog(LogItem log)
{

    QString outp="[";
    ColorTypes col=WHITE;
    switch(log.type)
    {
        case LERROR:
            col = LRED;
        break;
        case LINFO:
            col = LBLUE;
        break;
        case LSYS:
            col = LCYAN;
        break;
        case LALERT:
            col = YELLOW;
        break;
        case LDEBUG:
            col = BROWN;
        break;
        case IDEBUG:
            col = BROWN;
        break;
        case ODEBUG:
            col = BROWN;
        break;
        case IDEBUD:
            col = LRED;
        break;
        case ODEBUD:
            col = LRED;
        break;
    }

    SetColor(WHITE);
    std::cout << outp.toAscii().data();
    SetColor(col);
    std::cout << LogStr[(int)log.type].toAscii().data();
    SetColor(WHITE);
    std::cout << "] " << log.text.toAscii().data() << std::endl;
    if(logs.count()<2)
        std::cout << "TL2Server# ";
}

void Logger::SetColor(ColorTypes color)
{
#if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
static WORD WinColorFG[MaxColors] =
{
    0,                                                  // BLACK
    FOREGROUND_RED,                                     // RED
    FOREGROUND_GREEN,                                   // GREEN
    FOREGROUND_RED | FOREGROUND_GREEN,                  // BROWN
    FOREGROUND_BLUE,                                    // BLUE
    FOREGROUND_RED |                    FOREGROUND_BLUE, // MAGENTA
    FOREGROUND_GREEN | FOREGROUND_BLUE,                 // CYAN
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, // WHITE
                                                        // YELLOW
    FOREGROUND_RED | FOREGROUND_GREEN |                   FOREGROUND_INTENSITY,
                                                        // RED_BOLD
    FOREGROUND_RED |                                      FOREGROUND_INTENSITY,
                                                        // GREEN_BOLD
    FOREGROUND_GREEN |                   FOREGROUND_INTENSITY,
    FOREGROUND_BLUE | FOREGROUND_INTENSITY,             // BLUE_BOLD
                                                        // MAGENTA_BOLD
    FOREGROUND_RED |                    FOREGROUND_BLUE | FOREGROUND_INTENSITY,
                                                        // CYAN_BOLD
    FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
                                                        // WHITE_BOLD
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
};

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, WinColorFG[color]);
#else
    enum ANSITextAttr
    {
        TA_NORMAL                                = 0,
        TA_BOLD                                  = 1,
        TA_BLINK                                 = 5,
        TA_REVERSE                               = 7
    };

    enum ANSIFgTextAttr
    {
        FG_BLACK                                 = 30,
        FG_RED,
        FG_GREEN,
        FG_BROWN,
        FG_BLUE,
        FG_MAGENTA,
        FG_CYAN,
        FG_WHITE,
        FG_YELLOW
    };

    enum ANSIBgTextAttr
    {
        BG_BLACK                                 = 40,
        BG_RED,
        BG_GREEN,
        BG_BROWN,
        BG_BLUE,
        BG_MAGENTA,
        BG_CYAN,
        BG_WHITE
    };

    static quint8 UnixColorFG[MaxColors] =
    {
        FG_BLACK,                                          // BLACK
        FG_RED,                                            // RED
        FG_GREEN,                                          // GREEN
        FG_BROWN,                                          // BROWN
        FG_BLUE,                                           // BLUE
        FG_MAGENTA,                                        // MAGENTA
        FG_CYAN,                                           // CYAN
        FG_WHITE,                                          // WHITE
        FG_YELLOW,                                         // YELLOW
        FG_RED,                                            // LRED
        FG_GREEN,                                          // LGREEN
        FG_BLUE,                                           // LBLUE
        FG_MAGENTA,                                        // LMAGENTA
        FG_CYAN,                                           // LCYAN
        FG_WHITE                                           // LWHITE
    };

    //fprintf(0, "\x1b[%d%sm", UnixColorFG[color], (color >= YELLOW && color < MaxColors ? ";1" : ""));
    printf("\x1b[%d%sm", UnixColorFG[color], (color >= YELLOW && color < MaxColors ? ";1" : ""));
#endif

}


}
